package cc.idoone.springframework.placeholder.xml;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传配置管理
 *
 * @author hemj
 * @date 2018-05-17
 */
public class UploadConfiguration implements SmartInitializingSingleton {
    private static final Logger logger = LoggerFactory.getLogger(UploadConfiguration.class);
    // 默认配置信息
    @Value("${spring.upload.rule[default]}")
    private UploadRule defaultUploadRule; //default 必需
    // 配置信息(category ==> UploadRule)
    //@Value("${spring.upload}")
    private Map<String, UploadRule> rule = new HashMap<>();

    public UploadRule getUploadRule(String category) {
        return rule.get(category);
    }

    @Override
    public void afterSingletonsInstantiated() {
        checkConfig();
    }

    public Map<String, UploadRule> getRule() {
        return rule;
    }

    public void setRule(Map<String, UploadRule> rule) {
        this.rule = rule;
    }

    private void checkConfig() {
        UploadRule uploadRule = rule.remove("default");
        defaultUploadRule = uploadRule != null ? uploadRule : defaultUploadRule;
        if (defaultUploadRule == null) {
            logger.warn("默认上传配置(default)未设置");
            return;
        }
        logger.info("默认上传配置 \n{}\n", defaultUploadRule);

        checkUploadConfig(rule);
        logger.info("文件上传配置 \n{}\n", rule.values());
    }

    /**
     * 检查文档上传配置信息
     */
    private void checkUploadConfig(Map<String, UploadRule> uploadConfigs) {
        for (String key : uploadConfigs.keySet()) {
            UploadRule uploadRule = uploadConfigs.get(key);
            uploadRule.setCategory(key);
            uploadRule.setDefaultUploadRule(defaultUploadRule);
            // 删除配置信息不完整的配置项
            if (!checkUploadRule(uploadRule)) {
                uploadConfigs.remove(uploadRule.getCategory());
            }
        }
    }

    /**
     * 检查文档上传配置信息
     */
    private boolean checkUploadRule(UploadRule uploadRule) {
        if (!"oss".equals(uploadRule.getStoreMedia()) && !"disk".equals(uploadRule.getStoreMedia())) {
            logger.warn("{} storeMedia is error", uploadRule.getCategory());
            return false;
        }
        if (StringUtils.isBlank(uploadRule.getStoreBucket())) {
            logger.warn("{} storeBucket is empty", uploadRule.getCategory());
            return false;
        }
        if ("oss".equals(uploadRule.getStoreMedia())) {
            if (StringUtils.isBlank(uploadRule.getOssBucket())) {
                logger.warn("{} ossBucket is empty", uploadRule.getCategory());
                return false;
            }

        } else if ("disk".equals(uploadRule.getStoreMedia())) {
            if (StringUtils.isBlank(uploadRule.getStoreHome())) {
                logger.warn("{} uploadHome is empty", uploadRule.getCategory());
                return false;
            }
        }
        if (StringUtils.isBlank(uploadRule.getStorePath())) {
            logger.warn("{} storePath is empty", uploadRule.getCategory());
            return false;
        }
        if (uploadRule.getAllowTypes() == null) {
            logger.warn("{} allowTypes is null", uploadRule.getCategory());
            return false;
        }
        if (uploadRule.getDownloadPrefix() == null) {
            logger.warn("{} downloadPrefix is null", uploadRule.getCategory());
            return false;
        }
        return true;
    }
}
